package com.liuheqiao.sushegaunli.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import com.liuheqiao.sushegaunli.model.User;
import com.liuheqiao.sushegaunli.db.DBHelper;

public class UserDao {
    private DBHelper dbHelper;

    public UserDao(DBHelper dbHelper) {
        this.dbHelper = dbHelper;
    }

    // 添加用户
    public long insert(User user) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DBHelper.COLUMN_USERNAME, user.getUsername());
        values.put(DBHelper.COLUMN_PASSWORD, user.getPassword());
        values.put(DBHelper.COLUMN_PHONE, user.getPhone());
        values.put(DBHelper.COLUMN_STUDENT_ID, user.getStudentId());
        values.put(DBHelper.COLUMN_REAL_NAME, user.getRealName());
        values.put(DBHelper.COLUMN_BUILDING_NO, user.getBuildingNo());
        values.put(DBHelper.COLUMN_ROOM_NO, user.getRoomNo());
        values.put(DBHelper.COLUMN_ROLE, "student"); // 默认角色为学生
        
        return db.insert(DBHelper.TABLE_USERS, null, values);
    }

    // 检查用户名是否存在
    public boolean isUsernameExists(String username) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(DBHelper.TABLE_USERS, 
                new String[]{DBHelper.COLUMN_ID}, 
                DBHelper.COLUMN_USERNAME + " = ?",
                new String[]{username}, 
                null, null, null);
        boolean exists = cursor.getCount() > 0;
        cursor.close();
        return exists;
    }

    @SuppressLint("Range")
    public User login(String username, String password) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(DBHelper.TABLE_USERS, 
                null, 
                DBHelper.COLUMN_USERNAME + " = ? AND " + DBHelper.COLUMN_PASSWORD + " = ?",
                new String[]{username, password}, 
                null, null, null);
        
        User user = null;
        if (cursor.moveToFirst()) {
            user = new User();
            user.setId(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_ID)));
            user.setUsername(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_USERNAME)));
            user.setPassword(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PASSWORD)));
            user.setPhone(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PHONE)));
            user.setStudentId(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_STUDENT_ID)));
            user.setRealName(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_REAL_NAME)));
            user.setBuildingNo(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_BUILDING_NO)));
            user.setRoomNo(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_ROOM_NO)));
            user.setRole(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_ROLE)));
        }
        cursor.close();
        return user;
    }

    @SuppressLint("Range")
    public User findByUsername(String username) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(DBHelper.TABLE_USERS, 
                null, 
                DBHelper.COLUMN_USERNAME + " = ?",
                new String[]{username}, 
                null, null, null);
        
        User user = null;
        if (cursor.moveToFirst()) {
            user = new User();
            user.setId(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_ID)));
            user.setUsername(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_USERNAME)));
            user.setPassword(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PASSWORD)));
            user.setPhone(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PHONE)));
            user.setStudentId(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_STUDENT_ID)));
            user.setRealName(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_REAL_NAME)));
            user.setBuildingNo(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_BUILDING_NO)));
            user.setRoomNo(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_ROOM_NO)));
            user.setRole(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_ROLE)));
        }
        cursor.close();
        return user;
    }

    // 更新用户信息
    public int update(User user) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DBHelper.COLUMN_PASSWORD, user.getPassword());
        values.put(DBHelper.COLUMN_PHONE, user.getPhone());
        values.put(DBHelper.COLUMN_STUDENT_ID, user.getStudentId());
        values.put(DBHelper.COLUMN_REAL_NAME, user.getRealName());
        values.put(DBHelper.COLUMN_BUILDING_NO, user.getBuildingNo());
        values.put(DBHelper.COLUMN_ROOM_NO, user.getRoomNo());
        
        return db.update(DBHelper.TABLE_USERS, 
                values, 
                DBHelper.COLUMN_ID + " = ?", 
                new String[]{String.valueOf(user.getId())});
    }

    @SuppressLint("Range")
    public User getCurrentUser(Context context) {
        // 从 SharedPreferences 获取当前登录用户的用户名
        SharedPreferences prefs = context.getSharedPreferences("app_prefs", Context.MODE_PRIVATE);
        String username = prefs.getString("current_username", null);
        if (username == null) return null;

        return findByUsername(username);
    }

    // 保存当前用户信息
    public void saveCurrentUser(Context context, String username) {
        context.getSharedPreferences("app_prefs", Context.MODE_PRIVATE)
                .edit()
                .putString("current_username", username)
                .apply();
    }
} 